#!/bin/bash
# ----------------------------------------------------------------
#   1-CreateDirecotryStructure.sh: Create the directory structure
#   	and relocate each files to the right place
#
#	Usage:
#		1-CreateDirectoryStructure.sh rawDataSrc projectDir subjID
#   initializes the Adaptation fMRI experiment. This experiment 
#   effect of stimulus history on the current evoked response,
#   on serial position -1 to -N are manipulated in a 
#
#
#   Inputs
#   -------
#   [protocolListFileName]: path to a file that should contain a
#   	description of the protocols to be used. If nothing is
#   	the file ./Config/ProtocolList.cfg will be used
#
#
#   Outputs
#   -------
#   ./Data/[expType]/[protocol]/[subjID]/[subjID]-[protocol]-
#  	this file will store all experimental info, experiment
#   	parameters, stimuli used, and data collected
#
#   Examples
#   -------
#    i) AdaptID_v1_1('./protocols.txt')
#   ii) AdaptID_v1_1
#
# ----------------------------------------------------------------
# ``Redistribution and use, with or without modification, are
#	permitted provided that the list of contributors below is
#	preserved.``
#
#   Original code by Marcelo G Mattar (09/04/2012)
#	mattar@sas.upenn.edu
# ----------------------------------------------------------------


# --- Check input command ---
if [ $# -ne 3 ]; then
cat << EOF
USAGE: `basename $0` rawDataSrc projectDir subjID
EOF
	exit 1
fi

# --- Read input parameters ---
rawDataSrc=$1
projectDir=$2
subjID=$3

# --- Set up important variables ---
analysesDir=$projectDir/Analyses/$subjID
dataDir=$projectDir/Data/$subjID
rawDir=$dataDir/raw

# --- Keep a copy of calling directory, so that relative pathnames are properly used
OCD=$PWD


# -----------------------------------------------------
# 1) --- CHECK IF DIRECTORIES EXIST ---
# -----------------------------------------------------


# Check if source directory exist
if [ ! -d "$rawDataSrc" ]; then
	echo
	echo "WARNING: Source directory $rawDataSrc doesn't exist!!!"
	echo "Exiting..."
	echo
	exit 1
fi


if [ "$dataDir" = "$rawDataSrc" ]; then
	echo "Source directory is the same as Data directory."
	echo "Creating temporary directory..."
	mkdir $projectDir/Data/temp
	mv -f $rawDataSrc/* $projectDir/Data/temp
	rawDataSrc=$projectDir/Data/temp
else
	# Create Data directory
	if [ -d "$dataDir" ]; then
		echo
		echo "WARNING: Data directory $dataDir already exists!!!"
		read -p "Overwrite (y/n)? " -n 1
		if [[ ! $REPLY =~ ^[Yy]$ ]]
		then
			echo
			echo "Exiting..."
			echo
			exit 1
		fi
	else
		mkdir $dataDir
	fi
fi

echo

# Create Analyses directory
if [ -d "$analysesDir" ]; then
	echo
	echo "WARNING: Analyses directory $analysesDir already exists!!!"
	read -p "Overwrite (y/n)? " -n 1
	if [[ ! $REPLY =~ ^[Yy]$ ]]
	then
		echo
		echo "Exiting..."
		echo
		exit 1
	fi
else
	mkdir $analysesDir
fi

echo



# -----------------------------------------------------
# 2) --- CREATE DIRECTORY STRUCTURE ---
# -----------------------------------------------------

# Move all files inside source directory to parent directory
echo "Moving all raw files to parent directory"
cd $rawDataSrc
find -mindepth 2 -type f -print -exec mv {} . \;
# Remove DICOM directory and DICOMDIR file
rm -r -f DICOM*
# Recome all empty directories
find . -type d -depth -exec rmdir {} + 2>/dev/null

# Copy all files to the Data directory
echo "Creating backup copy of all raw files on $dataDir"
cp -f * $dataDir

# Create directory structure for that subject (add -n option to preserve subject information in the headers)
echo "Applying vbrename to $dataDir..."
vbrename $dataDir

# Create raw directory
if [ ! -d "$rawDir" ]; then
	mkdir $rawDir
fi

# Move all raw files to the Data directory and remove source directory
echo "Moving all raw files to $rawDir"
mv -f $rawDataSrc/* $rawDir/
echo "Removing $rawDataSrc"
rm -f $rawDataSrc/*
rmdir $rawDataSrc






cd $OCD

exit 0
